import pandas as pd
from emtencent import config as cn
import json
from urllib.request import urlopen, Request
import sys

# 千股千评
def get_qgqp(market=None):
    l = []
    if market and len(market) and isinstance(market, list):
        market_id_list = [cn.QGQP_DIC[key.upper()] for key in market if key in cn.QGQP_DIC.keys()]
        for market_id in market_id_list:
            l.extend(_get_qgqp(market_id))
    df = pd.DataFrame(l).T
    return df

def _get_qgqp(market_id=1):
    r = _get_qgqp_with_page(market_id=market_id)
    total_page = r[0]
    l = r[1]
    if total_page > 1:
        for i in range(2,total_page):
            r_ = _get_qgqp_with_page(market_id=market_id, page=i)
            l.extend(r_[1])
        sys.stdout.write('#' + "\b\b")
        sys.stdout.flush()
    return l



def _get_qgqp_with_page(market_id=1, page=1):
    req = Request(cn.QGQP %(page, market_id))
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = text.replace('var v_qgqp=', '').replace('\'ZQJC\'','\'name\'').replace('\'ZQDM\'','\'code\'')
    js = json.loads(text)
    totalpage = js['data']['totalPage']
    curPage = js['data']['curPage']
    result = js['data']['result']
    return (totalpage,result,curPage)


get_qgqp(market=['SH','SZ'])